from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.routes.auth import router as auth_router
from api.routes.translation import router as translation_router
from api.routes.history import router as history_router
from api.routes.medical_history import router as medical_history_router
from api.routes.feedback import router as feedback_router
from api.routes.content import router as content_router
from api.routes.terminology import router as terminology_router
import models.user  # 确保在创建表前加载模型
import models.translation_history
import models.medical_terms
import models.medical_history
import models.user_feedback
import models.content
from models.base import Base
from db import engine
from config import settings

# 创建FastAPI应用
app = FastAPI(
    title="三江译舟后端API",
    description="用户注册登录API - Python FastAPI项目",
    version="1.0.0"
)

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,

    allow_origins=["http://localhost:5173"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 创建数据库表（确保已导入所有模型）
Base.metadata.create_all(bind=engine)

# 注册路由
app.include_router(auth_router, prefix="/api/auth", tags=["认证"])
app.include_router(translation_router, prefix="/api/translation", tags=["翻译"])
app.include_router(history_router, prefix="/api/history", tags=["翻译历史"])

app.include_router(medical_history_router, prefix="/api/medical-history", tags=["病史"])
app.include_router(feedback_router, prefix="/api/feedback", tags=["反馈"])
app.include_router(content_router, prefix="/api/content", tags=["内容"])
app.include_router(terminology_router, prefix="/api/terminology", tags=["术语管理"])

@app.get("/")
def read_root():
    """根路径"""
    return {
        "message": "三江译舟后端API服务正在运行",
        "version": "1.0.0",
        "framework": "FastAPI",
        "database": "MySQL",
        "port": settings.PORT,
        "docs": f"http://localhost:{settings.PORT}/docs"
    }

@app.get("/health")
def health_check():
    """健康检查"""
    return {
        "status": "healthy",
        "database": "connected",
        "port": settings.PORT
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "main:app", 
        host=settings.HOST, 
        port=settings.PORT,
        reload=settings.DEBUG
    )
